home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-01
/
cexpert.zip
/
MCH9.LST
< prev
Wrap
File List
|
1990-09-15
|
8KB
|
479 lines
Listing 9-1 Sample Program to Read LISP Expressions
/*
** INPUT.C
**
**
** History:
**
** Routines to read lists in lisp fashion, i.e. of the form
** s_expression ::= atom | ( list )
** list ::= list atom |
**
*/
#include <ctype.h>
#include <stdio.h>
#include "cons.h"
#define LEFT_PAREN 64
#define RIGHT_PAREN 128
cons *lread(type,where)
int type;
char *where;
{
int dummy;
cons *read_s_expr();
return read_s_expr(type,where,&dummy);
}
static cons *read_s_expr(type,where,etype)
int type,*etype;
char *where;
{
int ty;
cons *f,*read_list();
char *strsave();
static char buf[512];
switch (get_token(type,where,buf)) {
case 0: /* empty list */
f = NULL; /* return a NIL pointer */
break;
case CAR_STRING: /* a string */
if (f = mkcons(CAR_STRING,strsave(buf),NULL)) {
*etype = CAR_STRING;
}
break;
case LEFT_PAREN:
f = read_list(type,where);
*etype = CAR_LIST;
break;
case RIGHT_PAREN:
f = (cons *) 1; /* for right parens */
*etype = ')';
break;
}
return f;
}
cons *read_list(type,where)
int type;
char *where;
{
cons *t, *z;
int first = 0,etype;
z = read_s_expr(type,where,&etype);
if (etype == ')') {
t = NULL;
} else {
if (t = mkcons(CAR_LIST,z,read_list(type,where))) {
return t;
} else {
t = NULL;
}
}
return t;
}
int get_token(type,ip,store)
int type; /* file or string input */
char *ip; /* input source */
char *store; /* place to store strings */
{
int c;
char *s;
c = skip_whitespace(type,ip);
if (c=='"') {
for (s=store;;s++) {
switch (*s = Getc(type,ip)) {
case '"':
if ((c = Getc(type,ip)) != '"') {
Ungetc(type,c,ip);
*s = '\0';
return CAR_STRING;
}
break;
case '\\':
switch (c=Getc(type,ip)) {
case 'n':
*s = '\n';
break;
case 'r':
*s = '\r';
break;
case 'd':
*s = '\004';
break;
/* case '0':
Ungetc(type,c,ip);
*s = (char) c;
break;
*/
case 'p':
*s++ = '\\';
*s = 'p';
break;
case '(':
*s++ = '\\';
*s = '(';
break;
case ')':
*s++ = '\\';
*s = ')';
break;
case '\0':
*s = '\0';
return CAR_STRING;
break;
default:
*s = c;
break;
}
break;
}
}
}
if (isalpha(c) || c == '?') {
Ungetc(type,c,ip);
for (s=store; isalpha(*s=Getc(type,ip)) || isdigit(*s) || *s == '-'
|| *s == '?'; s++)
;
Ungetc(type,*s,ip);
*s='\0';
return CAR_STRING;
}
if (isdigit(c)) {
int n = c - '0';
/*
while (c = Getc(type,ip),isdigit(c)) {
n *= 10;
n += c - '0';
}
Ungetc(type,c,ip);
*((int *) store) = n;
*/
*store++ = c;
while (c = Getc(type,ip),isdigit(c) || c == '.') {
*store++ = c;
}
*store = '\0';
Ungetc(type,c,ip);
return CAR_STRING;
}
if (c == '(') {
if ((c = skip_whitespace(type,ip)) == ')') {
return 0; /* empty list */
} else {
Ungetc(type,c,ip);
return LEFT_PAREN;
}
}
if (c == ')') {
return RIGHT_PAREN;
}
return c;
}
char *strsave(s)
char *s;
{
char *t;
if (t = (char *) malloc(strlen(s)+1)) {
strcpy(t,s);
}
return t;
}
int Getc(type,source)
int type;
char *source;
{
if (type == C_STRING) {
return (*(*((char **) source))++); /* whew! */
} else {
return getc((FILE *) source);
}
}
int Ungetc(type,c,source)
int type;
char c;
char *source;
{
if (type == C_STRING) {
*--(*((char **) source)) = c;
} else {
ungetc(c,(FILE *) source);
}
}
int skip_whitespace(type,ip)
int type;
char *ip;
{
int c;
while ((c = Getc(type,ip)) == ' ' || c == '\t' || c == '\n' || c == ';')
if (c == ';') /* a comment */
while (Getc(type,ip) != '\n') /* read up to & including the EOL */
;
return c;
}
Listing 9-2 Sample Program to Print Output in LISP Fashion
/*
** OUTPUT.C
**
**
** Routines to print lists in lisp fashion.
**
*/
#include <stdio.h>
#include "cons.h"
static int sputs(s,where) /* similar to fputs */
char *s, *where;
{
strcat(where,s);
return 0;
}
/****************/
lprint(l,type,target)
cons *l;
int type;
char *target;
{
switch (type) {
case C_STRING:
_print_s_expr(l,sputs,target);
break;
case C_FILE:
_print_s_expr(l,fputs,target);
break;
default:
; /**/ /* Error-message */
break;
}
}
/************/
static _print_s_expr(node,output,target)
cons *node;
int (*output)();
char *target;
{
static char slask[80];
if (node->type == 0 & node->car.p == NULL) {
printf("()");
} else if (node->type == CAR_STRING) {
(*output) ((char *) node->car.s,target);
} else if (node->type == CAR_LIST) {
_print_list(node,output,target);
} else {
; /**/ /* Error message */
}
}
/*
** _print_list():
*/
static _print_list(node,output,target)
cons *node;
int (*output)();
char *target;
{
(*output) ("(",target);
do {
_print_s_expr((cons *) node->car.p,output,target);
node = node->cdr;
(*output) ((node == NULL) ? ")" : " ",target);
} while (node != NULL);
}
Listing 9-3 Sample Program for Creating Windows
/*
** WINDOW.C
**
** Supports a little window in the region (1,19) - (78,22) on the screen.
** Diagnostic output from REMOTE will be routed to this window. It is
** deliberately very stupid, and will only accept <CR>, <LF> and <BS> as
** valid control characters. Everything else is output directly to the
** screen in the region specified above.
*/
#include <dos.h>
#define LOX 1
#define HIX 78
#define LOY 19
#define HIY 22
#define WIN_COLOR 0x0E
#define VIDEO 0x10
static int winx, winy,display_page;
wclear()
{
union REGS in,out;
in.h.ah = 0x0F; /* get current video mode */
int86(VIDEO,&in,&out);
display_page = out.h.bh; /* current screen */
out.h.ah = 0x02; /* set cursor position */
out.h.bh = display_page;
out.h.dl = winx = LOX; /* move to home position */
out.h.dh = winy = HIY;
int86(VIDEO,&out,&in);
}
/*
** wputs(): John's window puts: put string to window using BIOS services
*/
wputs(s) /* put string using BIOS IO */
char *s;
{
while (*s) {
wputc(*s++);
}
}
wputc(c)
char c;
{
union REGS regs;
if (winy < LOY) {
winy = LOY;
winx = LOX;
}
regs.h.ah = 0x02; /* set cursor position */
regs.h.bh = display_page;
regs.h.dl = winx; /* move to home position */
regs.h.dh = winy;
int86(VIDEO,®s,®s);
switch (c) {
case '\b':
if (winx > LOX)
--winx;
break;
case '\r':
winx = LOX;
break;
case '\n':
wnl();
break;
default:
regs.h.ah = 0x09; /* draw character */
regs.h.al = c;
regs.h.bl = WIN_COLOR;
regs.h.bh = display_page;
regs.x.cx = 1; /* only 1 char */
int86(VIDEO,®s,®s); /* dumpit */
if (++winx > HIX) {
winx = LOX;
wnl();
}
}
regs.h.ah = 0x02; /* set cursor position */
regs.h.bh = display_page;
regs.h.dl = winx; /* move to home position */
regs.h.dh = winy;
int86(VIDEO,®s,®s);
}
wnl() {
union REGS in;
if (winy < HIY) { /* not at bottom line */
++winy;
} else {
winy = HIY; /* force to bottom line */
in.h.ah = 0x06; /* scroll window up */
in.h.bh = display_page; /* current screen */
in.h.al = 1; /* scroll one line */
in.h.cl = LOX; /* region. */
in.h.ch = LOY;
in.h.dl = HIX;
in.h.dh = HIY;
in.h.bh = WIN_COLOR;
int86(VIDEO,&in,&in);
}
}
Listing 9-4 Sample Program to Compute the Minimum Certainty
/*
** MIN.C
**
***/
#include <stdio.h>
#include "cons.h"
#include "goal.h"
/*-------------------------------------------------Min()-------------------*/
/*
** Get the minimum certaity between two.
*/
double Min(cert1,cert2,cert3)
double cert1,cert2,*cert3;
{
if(cert1 <= cert2)
{
(*cert3) = cert1;
}
else
{
(*cert3) = cert2;
}
}